package com.dq.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dq.commonutils.vo.Result;
import com.dq.statistics.client.UcenterClient;
import com.dq.statistics.entity.StatisticsDaily;
import com.dq.statistics.service.StatisticsDailyService;
import com.dq.statistics.mapper.StatisticsDailyMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dq.statistics.vo.ChartsParams;
import com.dq.statistics.vo.ChartsVo;
import org.apache.commons.lang3.RandomUtils;
import org.bouncycastle.pqc.math.linearalgebra.RandUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 网站统计日数据(StatisticsDaily)表服务实现类
 *
 * @author Dq
 * @since 2022-09-15 16:38:30
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {
    @Autowired
    private UcenterClient ucenterClient;

    @Autowired
    private StatisticsDailyMapper staMapper;

    //查询某一天注册人数 然后把这个数据条数放入数据库（用于图形化显示）
    @Override
    public void registerCount(String day) {
        //远程调用获取这次查询的count
        Result countDate = ucenterClient.findTotalRegisterCountFromOneDay(day);
        Map<String, Integer> data = (Map<String, Integer>) countDate.getData();
        Integer count = data.get("count");


        LambdaQueryWrapper<StatisticsDaily> statisticsDailyLambdaQueryWrapper = new LambdaQueryWrapper<>();
        statisticsDailyLambdaQueryWrapper.eq(StatisticsDaily::getDateCalculated, day).select(StatisticsDaily::getId);
        StatisticsDaily statisticsDaily = staMapper.selectOne(statisticsDailyLambdaQueryWrapper);

        //插入数据
        StatisticsDaily daily = new StatisticsDaily();
        if (statisticsDaily == null) { //进行查询操作 如果查无此数据
            daily.setRegisterNum(count); //某个时间段的注册人数
            daily.setDateCalculated(day);

            daily.setCourseNum(RandomUtils.nextInt(100, 200));
            daily.setVideoViewNum(RandomUtils.nextInt(100, 200));
            daily.setLoginNum(RandomUtils.nextInt(100, 200));
            staMapper.insert(daily);
        } else { //更新数据
            daily.setRegisterNum(count);
            daily.setId(statisticsDaily.getId());
            staMapper.updateById(daily);
        }

    }

    @Override
    public List<ChartsVo> getChartsDate(ChartsParams chartsParams) {

        QueryWrapper<StatisticsDaily> queryWrapper = new QueryWrapper<>();
        /*if (chartsParams==null){
            queryWrapper.orderByDesc("date_calculated").last("limit 10");
        }*/
        if (!chartsParams.getStart().isEmpty() && !chartsParams.getEnd().isEmpty()){
            queryWrapper.between("date_calculated", chartsParams.getStart(), chartsParams.getEnd());
        }else {
            queryWrapper.orderByDesc("date_calculated").last("limit 7");
        }
        if (!chartsParams.getType().isEmpty()){
            queryWrapper.select("date_calculated",chartsParams.getType(),"id");
        }
        List<StatisticsDaily> statisticsDailies = staMapper.selectList(queryWrapper);
        List<ChartsVo> chartsVos = ListChange(statisticsDailies);

        return chartsVos;
    }

    public static List<ChartsVo> ListChange(List<StatisticsDaily> statisticsDailies) {
        List<ChartsVo> chartsVos = new ArrayList<>();
        for (StatisticsDaily statisticsDaily : statisticsDailies) {
            chartsVos.add(OneChange(statisticsDaily));
        }
        return chartsVos;
    }

    public static ChartsVo OneChange(StatisticsDaily statisticsDaily) {
        ChartsVo chartsVo = new ChartsVo();
        BeanUtils.copyProperties(statisticsDaily, chartsVo);
        return chartsVo;
    }
}